package com.marvinlabs.widget.slideshow.demo.slice;

import com.marvinlabs.widget.slideshow.SlideShowAdapter;
import com.marvinlabs.widget.slideshow.SlideShowView;
import com.marvinlabs.widget.slideshow.adapter.RemoteBitmapAdapter;
import com.marvinlabs.widget.slideshow.adapter.ResourceBitmapAdapter;
import com.marvinlabs.widget.slideshow.demo.ResourceTable;
import com.marvinlabs.widget.slideshow.util.HeLog;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.common.Size;

import java.util.Arrays;

public class MainAbilitySlice extends AbilitySlice {
    private SlideShowView slideShowView;
    private SlideShowAdapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        init();
    }

    @Override
    public void onActive() {
        super.onActive();
        slideShowView.setLayoutRefreshedListener(component -> {
            if (component.getHeight() > 0 && adapter == null) {
                startSlideShow();
            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void init() {
        // Go fullscreen
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);

        slideShowView = (SlideShowView) findComponentById(ResourceTable.Id_slideshow);
    }

    private SlideShowAdapter createRemoteAdapter() {
        String[] slideUrls = new String[]{
                "https://i0.hdslb.com/bfs/article/b078eb487a6b5ea8d40c9d7779cfb1c7ae9c504a.jpg@1320w_2762h.webp",
                "https://i0.hdslb.com/bfs/article/73360351983a98ecdfd11b3774ceca83b8e0cd70.jpg@1320w_2640h.webp",
                "https://i0.hdslb.com/bfs/article/0a92b622e3e730f1ed77360f6d49baceff79c781.jpg@1320w_2742h.webp",
                "https://i0.hdslb.com/bfs/article/e62c0565e19b8ca5121cebab507d66d824702896.jpg@1320w_2860h.webp",
        };

        ImageSource.DecodingOptions opts = new ImageSource.DecodingOptions();
        opts.desiredSize = new Size(slideShowView.getWidth(), slideShowView.getHeight());

        //使用网络图片
        adapter = new RemoteBitmapAdapter(this, Arrays.asList(slideUrls), opts);
        return adapter;
    }

    private SlideShowAdapter createResourceAdapter() {
        Integer[] slideUrlsInt = new Integer[]{
                ResourceTable.Media_s1,
                ResourceTable.Media_s2,
                ResourceTable.Media_s3,
                ResourceTable.Media_s4,
        };

        ImageSource.DecodingOptions opts = new ImageSource.DecodingOptions();
        opts.desiredSize = new Size(slideShowView.getWidth(), slideShowView.getHeight());

        //使用本地图片
        adapter = new ResourceBitmapAdapter(this, Arrays.asList(slideUrlsInt), opts);
        return adapter;
    }

    private void startSlideShow() {
        // Create an adapter
        slideShowView.setAdapter(createResourceAdapter());
//        slideShowView.setAdapter(createRemoteAdapter());

        // Optional customisation follows
        // slideShowView.setTransitionFactory(new RandomTransitionFactory());
        // slideShowView.setPlaylist(new RandomPlayList());

        // Some listeners if needed
        slideShowView.setOnSlideShowEventListener(slideShowListener);
        slideShowView.setOnSlideClickListener(slideClickListener);

        // Then attach the adapter
        slideShowView.play();
    }

    private SlideShowView.OnSlideClickListener slideClickListener = new SlideShowView.OnSlideClickListener() {
        @Override
        public void onItemClick(SlideShowView parent, int position) {
            showCustomText(getContext(), "Slide clicked: " + position);
        }
    };

    private SlideShowView.OnSlideShowEventListener slideShowListener = new SlideShowView.OnSlideShowEventListener() {
        @Override
        public void beforeSlideShown(SlideShowView parent, int position) {
            HeLog.d("SlideShowDemo", "OnSlideShowEventListener.beforeSlideShown: " + position);
        }

        @Override
        public void onSlideShown(SlideShowView parent, int position) {
            HeLog.d("SlideShowDemo", "OnSlideShowEventListener.onSlideShown: " + position);
        }

        @Override
        public void beforeSlideHidden(SlideShowView parent, int position) {
            HeLog.d("SlideShowDemo", "OnSlideShowEventListener.beforeSlideHidden: " + position);
        }

        @Override
        public void onSlideHidden(SlideShowView parent, int position) {
            HeLog.d("SlideShowDemo", "OnSlideShowEventListener.onSlideHidden: " + position);
        }
    };

    public void showCustomText(Context context, String toastStr) {
        Text textComponent = new Text(context);
        textComponent.setText(toastStr);

        int padding = 40;
        //设置间距为10vp
        textComponent.setPadding(60, 40, 60, 40);
        textComponent.setTextColor(Color.BLACK);
        textComponent.setWidth(500);
        textComponent.setTextAlignment(TextAlignment.CENTER);
        textComponent.setTextSize(50);

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        shapeElement.setCornerRadius(50);
        //设置背景半透明
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.argb(210, 255, 255, 255)));
        textComponent.setBackground(shapeElement);
        //设置文字允许多行
        textComponent.setMultipleLine(true);

        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
        layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        layoutConfig.alignment = LayoutAlignment.CENTER;
        layoutConfig.setMarginBottom(220);
        textComponent.setLayoutConfig(layoutConfig);

        ToastDialog toastDialog = new ToastDialog(context).setDuration(500).setComponent(textComponent).setAlignment(LayoutAlignment.BOTTOM);
        //设置弹框背景透明
        toastDialog.setTransparent(true);
        toastDialog.show();
    }
}
